package org.itboys.mobile.service.mongo.weixin.address;

import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.entity.mongo.SundayAddress;
import org.itboys.mobile.entity.mongo.SundayMember;
import org.itboys.mobile.service.mongo.web.member.SundayMemberService;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created on 2017/11/30.
 * @author 朱坤涛
 */
@Service
public class SundayWeixinAddressService extends BaseMongoService<SundayAddress> {

    @Autowired
    private SundayMemberService memberService;
    @Resource(name = "mobileDs")
    private MongoDataSource sundayDataSource;
    @Override
    protected MongoDataSource getMongoDataSource() {
        return sundayDataSource;
    }

    @Override
    protected Class<SundayAddress> getEntityClass() {
        return SundayAddress.class;
    }

    /**
     * 获取用户所有收货地址
     * @param memberId
     * @return
     */
    public List<SundayAddress> selectAddress(Long memberId){
        Map<String,Object> param =new HashMap<String, Object>();
        param.put("memberId", memberId);
        param.put("isDeleted", SundayCommonConstants.NO);
        //有限排序默认地址
        param.put("sort","isDefault");
        param.put("order","DESC");
        SundayMember member=memberService.getById(memberId);
        List<SundayAddress> adds = super.list(param);
        return adds;
    }

    /**
     * 新增收货地址(用户版和管理版)。农厨版本收货地址：用户：传入小区ID；门店（大客户）：传入省市区
     * @param id
     * @param memberId
     * @param receiveName
     * @param receiveMobile
     * @param provinceId
     * @param provinceName
     * @param cityId
     * @param cityName
     * @param districtId
     * @param districtName
     * @param receiveAddress
     */
    public void saveAddress(Long id,Long memberId,String receiveName,String receiveMobile,
                            Long provinceId, String provinceName, Long cityId, String cityName, Long districtId, String districtName,
                            String receiveAddress,Integer isDefault){

        Map<String,Object> param=new HashMap<String,Object>();
        param.put("isDeleted",0);
        param.put("memberId",memberId);
        if(isDefault==1){
            List<SundayAddress> addresses=super.list(param);
            if(ListUtil.isNotNull(addresses)){
                for(SundayAddress address:addresses){
                    address.setIsDefault(0);
                    super.updateExceptEmpty(address.getId(),address);
                }
            }
        }
        if(id==null||id==0l){
            SundayAddress address=new SundayAddress(
                    id,memberId, receiveName,receiveMobile,
                    provinceId, provinceName,cityId,cityName,districtId,districtName,
                    receiveAddress, isDefault);
            super.save(address);

        }else {
            super.updateExceptEmpty(id,new SundayAddress(id,memberId,receiveName,receiveMobile,provinceId, provinceName,cityId,cityName,districtId,districtName,receiveAddress, isDefault));
        }
    }

    /**
     * 设置默认地址
     * @param addressId
     * @param memberId
     */
    public void setDefaultAddress(Long addressId,Long memberId){
        Map<String, Object> param =new HashMap<String,Object>();
        param.put("isDeleted", SundayCommonConstants.NO);
        param.put("memberId",memberId);
        List<SundayAddress> addresses = super.list(param);
        for(SundayAddress address:addresses){
            if(address.getId()==addressId){
                address.setIsDefault(SundayCommonConstants.YES);
                super.updateExceptEmpty(address.getId(),address);
            }else{
                address.setIsDefault(SundayCommonConstants.NO);
                super.updateExceptEmpty(address.getId(),address);
            }
        }
    }
    /**
     * 删除收货地址
     * @param addressId
     * @param memberId
     */
    public void deleteAddress(Long addressId,Long memberId,String nonce, String sign){
        //2017年6月20日，新增，验证签名
        Map<String, Object> param =new HashMap<String,Object>();
        param.clear();
        param.put("isDeleted", SundayCommonConstants.NO);
        param.put("memberId",memberId);

        List<SundayAddress> addresses = super.list(param);

        super.update(addressId,"isDeleted", SundayCommonConstants.YES);
    }
    /**
     * 获取默认的地址
     * @param memberId
     * @return
     */
    public SundayAddress getDefaultAddress(Long memberId){
        Map<String,Object> param =new HashMap<String, Object>();
        param.put("isDeleted", SundayCommonConstants.NO);
        param.put("isDefault", SundayCommonConstants.YES);
        param.put("memberId", memberId);
        List<SundayAddress> addes = super.list(param);
        if(ListUtil.isNotNull(addes))return addes.get(0);
        return null;
    }

}
